package com.common.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.common.utils.StarAppUtils;
import com.starappallcommon.R;

/**
 * Toast
 * 
 * <br>
 * Created 2014-6-9 下午5:44:02
 * 
 * @version
 * @author linsj
 * 
 * @see
 */
public class StarappToast extends Toast {

    private final static int HEIGHT = 85;
    private final static int HEIGHT_TOP = 58;

    /**
     * 
     * Creates a new instance of StarappToast. <br>
     * Created 2014-6-9 下午5:42:54
     * 
     * @param context
     *            上下文
     */
    public StarappToast(Context context) {
        super(context);
    }

    /**
     * 
     * Creates a new instance of StarappToast. <br>
     * Created 2014-6-9 下午5:43:04
     * 
     * @param context
     *            上下文
     * @param attrs
     *            属性值
     */
    public StarappToast(Context context, AttributeSet attrs) {
        super(context);
    }

    /**
     * 显示toast
     * 
     * @param context
     *            上下文
     * @param text
     *            提示消息
     * @param duration
     *            时长
     * @return
     */
    public static void show(Context context, CharSequence text, int duration) {
        StarappToast result = new StarappToast(context);
        LayoutInflater inflate = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        DisplayMetrics dm = context.getResources().getDisplayMetrics();
        View v = inflate.inflate(R.layout.starapp_toast, null);
        TextView tv = (TextView) v.findViewById(R.id.tv_toast);
        StarAppUtils.setViewTypeFace(tv, false);
        tv.setText(text);
        result.setView(v);
        result.setDuration(duration);
        result.setGravity(Gravity.BOTTOM, 0, (int) (dm.density * HEIGHT));
        result.show();
    }

    /**
     * 显示在中间的toast <br>
     * Created 2014-9-30 下午1:57:40
     * 
     * @param context
     *            上下文
     * @param text
     *            提示消息
     * @param duration
     *            时长
     * @author gpy
     */
    public static void showInCenter(Context context, CharSequence text, int duration) {
        StarappToast result = new StarappToast(context);
        LayoutInflater inflate = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        DisplayMetrics dm = context.getResources().getDisplayMetrics();
        View v = inflate.inflate(R.layout.starapp_toast_top, null);
        TextView tv = (TextView) v.findViewById(R.id.tv_toast);
        StarAppUtils.setViewTypeFace(tv, false);
        tv.setText(text);
        result.setView(v);
        result.setDuration(duration);
        result.setGravity(Gravity.CENTER, 0, 0);
        result.show();
    }

    /**
     * 显示toast,可自定义背景及图标
     * 
     * <br>
     * Created 2014-6-6 下午5:41:35
     * 
     * @param context
     *            上下文
     * @param text
     *            提示文字
     * @param bkgnResid
     *            背景资源ID
     * @param iconResid
     *            图标资源ID
     * @param duration
     *            显示时长
     * @author : linsj
     */
    public static void show(Context context, CharSequence text, int bkgnResid, int iconResid,
            int duration) {
        StarappToast result = new StarappToast(context);
        LayoutInflater inflate = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        DisplayMetrics dm = context.getResources().getDisplayMetrics();
        View v = inflate.inflate(R.layout.starapp_toast, null);
        v.setBackgroundResource(bkgnResid);

        ImageView ivIcon = (ImageView) v.findViewById(R.id.iv_icon);
        ivIcon.setBackgroundResource(iconResid);

        TextView tv = (TextView) v.findViewById(R.id.tv_toast);
        tv.setText(text);
        StarAppUtils.setViewTypeFace(tv, false);
        result.setView(v);
        result.setDuration(duration);
        result.setGravity(Gravity.BOTTOM, 0, (int) (dm.density * HEIGHT));
        result.show();
    }
    
    /**
     * 显示toast,可自定义背景及图标
     * 
     * <br>
     * Created 2014-6-6 下午5:41:35
     * 
     * @param context
     *            上下文
     * @param text
     *            提示文字
     * @param bkgnResid
     *            背景资源ID
     * @param iconResid
     *            图标资源ID
     * @param duration
     *            显示时长
     * @author : zhangqr
     */
    public static void showInCenter(Context context, CharSequence text,
            int iconResid, int duration) {
        StarappToast result = new StarappToast(context);
        LayoutInflater inflate = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        DisplayMetrics dm = context.getResources().getDisplayMetrics();
        View v = inflate.inflate(R.layout.starapp_toast, null);

        ImageView ivIcon = (ImageView) v.findViewById(R.id.iv_icon);
        if (iconResid == 0) {
            ivIcon.setVisibility(View.GONE);
        } else {
            ivIcon.setVisibility(View.VISIBLE);
            ivIcon.setBackgroundResource(iconResid);
        }

        TextView tv = (TextView) v.findViewById(R.id.tv_toast);
        tv.setText(text);
        StarAppUtils.setViewTypeFace(tv, false);
        result.setView(v);
        result.setDuration(duration);
        result.setGravity(Gravity.CENTER, 0, 0);
        result.show();
    }

    /**
     * 显示toast,可自定义背景及图标
     *
     * <br>
     * Created 2014-6-6 下午5:41:35
     *
     * @param context
     *            上下文
     * @param text
     *            提示文字
     * @param bgResid
     *            背景资源ID
     * @param textColor
     *            文字颜色
     * @param iconResid
     *            图标资源ID
     * @param duration
     *            显示时长
     * @author : zhangqr
     */
    public static void showInCenter(Context context, CharSequence text,
            int iconResid,int bgResid, int textColor, int duration) {
        StarappToast result = new StarappToast(context);
        LayoutInflater inflate = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        DisplayMetrics dm = context.getResources().getDisplayMetrics();
        View v = inflate.inflate(R.layout.starapp_toast, null);
        if (bgResid > 0) {
            v.setBackgroundResource(bgResid);
        }
        ImageView ivIcon = (ImageView) v.findViewById(R.id.iv_icon);
        if (iconResid == 0) {
            ivIcon.setVisibility(View.GONE);
        } else {
            ivIcon.setVisibility(View.VISIBLE);
            ivIcon.setBackgroundResource(iconResid);
        }

        TextView tv = (TextView) v.findViewById(R.id.tv_toast);
        tv.setText(text);
        tv.setTextColor(textColor);
        StarAppUtils.setViewTypeFace(tv, false);
        result.setView(v);
        result.setDuration(duration);
        result.setGravity(Gravity.CENTER, 0, 0);
        result.show();
    }

    /**
     * 显示通用的错误提示toast
     * 
     * <br>
     * Created 2014-9-15 下午6:27:39
     * 
     * @param context
     *            上下文
     * @param csToast
     *            错误提示
     * @param duration
     *            停留时间
     * @author linsj
     */
    public static void showCommonFailToast(Context context, CharSequence csToast1, int duration) {
        StarappToast result = new StarappToast(context);
        LayoutInflater inflate = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        DisplayMetrics dm = context.getResources().getDisplayMetrics();
        View v = inflate.inflate(R.layout.starapp_toast_operate_fail2, null);

        TextView tv = (TextView) v.findViewById(R.id.tv_2toast1);
        tv.setText(csToast1);
        StarAppUtils.setViewTypeFace(tv, false);
        // tv = (TextView) v.findViewById(R.id.tv_toast2);
        // tv.setText(csToast2);
        // StarAppUtils.setViewTypeFace(tv, false);

        result.setView(v);
        result.setDuration(duration);
        result.setGravity(Gravity.CENTER, 0, 0);
        result.show();
    }

    /**
     * 显示通用的错误提示toast
     * 
     * <br>
     * Created 2014-9-15 下午6:27:39
     * 
     * @param context
     *            上下文
     * @param csToast
     *            错误提示
     * @param duration
     *            停留时间
     * @author linsj
     */
    public static void showCommonFailToast(Context context, CharSequence csToast1,
            CharSequence csToast2, int duration) {
        StarappToast result = new StarappToast(context);
        LayoutInflater inflate = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        DisplayMetrics dm = context.getResources().getDisplayMetrics();
        View v = inflate.inflate(R.layout.starapp_toast_operate_fail, null);

        TextView tv = (TextView) v.findViewById(R.id.tv_toast1);
        tv.setText(csToast1);
        StarAppUtils.setViewTypeFace(tv, false);
        tv = (TextView) v.findViewById(R.id.tv_toast2);
        tv.setText(csToast2);
        StarAppUtils.setViewTypeFace(tv, false);

        result.setView(v);
        result.setDuration(duration);
        result.setGravity(Gravity.CENTER, 0, 0);
        result.show();
    }
}
